package com.shaoshaohuo.app.wxapi;


import android.content.Intent;
import android.os.Bundle;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.PayResultEneity;
import com.shaoshaohuo.app.framework.Logger;
import com.shaoshaohuo.app.framework.Logger.LogTag;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.ui.pay.PayResultActivity;
import com.shaoshaohuo.app.ui.pay.RechargeResultActivity;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler{
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
    /**
     * 当前正在进行的微信支付对象
     */
    private static PayResultEneity currentPayEntity;
    /**
     * 充值
     */
    public static final String PAY_RECHAEGE = "PAY_RECHAEGE";
    /**
     * 电商
     */
    public static final String PAY_ORDER_EC = "PAY_ORDER_EC";
    /**
     * 物流
     */
    public static final String PAY_ORDER_SHIPPER = "PAY_ORDER_SHIPPER";
    /**
     * 当前的支付方式
     */
    private static String currentPayWay;

    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        
    	api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);

        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        Logger.d(LogTag.UI, resp.errStr+"-"+resp.errCode+"-"+resp.openId+"-"+resp.transaction);
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if(resp.errCode == 0){
                if (PAY_RECHAEGE.equals(getCurrentPayWay())) {
                    Intent successIntent = new Intent(WXPayEntryActivity.this, RechargeResultActivity.class);
                    successIntent.putExtra(PayResultActivity.KEY_RESULT, RechargeResultActivity.VALUE_SUCCESS);
                    successIntent.putExtra(PayResultActivity.KEY_PAY_WAY, "微信");
                    successIntent.putExtra(PayResultActivity.FORWARD_KEY, RechargeResultActivity.FORWARD_USER_MAIN);
                    successIntent.putExtra(PayResultActivity.KEY_MONEY, getCurrentPayEntity().getData().getMoney() + "元");
                    successIntent.putExtra(PayResultActivity.KEY_ORDER_ID, getCurrentPayEntity().getData().getOrderid());
                    successIntent.putExtra(PayResultActivity.KEY_PAY_TIME, getCurrentPayEntity().getData().getPaytime());
                    startActivity(successIntent);
                } else if (PAY_ORDER_EC.equals(getCurrentPayWay())) {
                    Intent successIntent = new Intent(WXPayEntryActivity.this, PayResultActivity.class);
                    successIntent.putExtra(PayResultActivity.KEY_RESULT, PayResultActivity.VALUE_SUCCESS);
                    successIntent.putExtra(PayResultActivity.KEY_PAY_WAY, "微信");
                    successIntent.putExtra(PayResultActivity.FORWARD_KEY, PayResultActivity.FORWARD_EC_ORDER_LIST);
                    successIntent.putExtra(PayResultActivity.KEY_MONEY, getCurrentPayEntity().getData().getMoney() + "元");
                    successIntent.putExtra(PayResultActivity.KEY_ORDER_ID, getCurrentPayEntity().getData().getOrderid());
                    successIntent.putExtra(PayResultActivity.KEY_PAY_TIME, getCurrentPayEntity().getData().getPaytime());
                    startActivity(successIntent);
                } else if (PAY_ORDER_SHIPPER.equals(getCurrentPayWay())) {
                    Intent successIntent = new Intent(WXPayEntryActivity.this, PayResultActivity.class);
                    successIntent.putExtra(PayResultActivity.KEY_RESULT, PayResultActivity.VALUE_SUCCESS);
                    successIntent.putExtra(PayResultActivity.KEY_PAY_WAY, "微信");
                    successIntent.putExtra(PayResultActivity.FORWARD_KEY, PayResultActivity.FORWARD_CART_ORDER_LIST);
                    successIntent.putExtra(PayResultActivity.KEY_MONEY, getCurrentPayEntity().getData().getMoney() + "元");
                    successIntent.putExtra(PayResultActivity.KEY_ORDER_ID, getCurrentPayEntity().getData().getOrderid());
                    successIntent.putExtra(PayResultActivity.KEY_PAY_TIME, getCurrentPayEntity().getData().getPaytime());
                    startActivity(successIntent);
                }
                setCurrentPayEntity(null);
                setCurrentPayWay(null);
                finish();
                return;
            }else if(resp.errCode == -1){
                String reason = "支付错误";
                if (PAY_RECHAEGE.equals(getCurrentPayWay())) {
                	Intent successIntent = new Intent(WXPayEntryActivity.this, RechargeResultActivity.class);
                	successIntent.putExtra(PayResultActivity.FORWARD_KEY, PayResultActivity.FORWARD_USER_MAIN);
                	successIntent.putExtra(PayResultActivity.KEY_RESULT, PayResultActivity.VALUE_FAILURE);
                	successIntent.putExtra(PayResultActivity.KEY_FAILURE_REASON, reason);
                	startActivity(successIntent);
                }
            	finish();
            }else if(resp.errCode == -2){
                String reason = "支付取消";
                if (PAY_RECHAEGE.equals(getCurrentPayWay())) {
                	Intent successIntent = new Intent(WXPayEntryActivity.this, RechargeResultActivity.class);
                	successIntent.putExtra(PayResultActivity.FORWARD_KEY, PayResultActivity.FORWARD_USER_MAIN);
                	successIntent.putExtra(PayResultActivity.KEY_RESULT, PayResultActivity.VALUE_FAILURE);
                	successIntent.putExtra(PayResultActivity.KEY_FAILURE_REASON, reason);
                	startActivity(successIntent);
                }
            	finish();
            }
        }
        finish();
    }

    public static PayResultEneity getCurrentPayEntity() {
        return currentPayEntity;
    }

    public static void setCurrentPayEntity(PayResultEneity currentPayEntity) {
        WXPayEntryActivity.currentPayEntity = currentPayEntity;
    }

    public static String getCurrentPayWay() {
        return currentPayWay;
    }

    public static void setCurrentPayWay(String currentPayWay) {
        WXPayEntryActivity.currentPayWay = currentPayWay;
    }
}